package com.sq.universe.system.controller;

import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.TableConfigurationEntity;
import com.sq.universe.system.service.AutoCodeService;
import com.sq.universe.system.service.TableConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/autoCode")
@CrossOrigin
@Api(description = "代碼生產")
public class AutoCodeController extends BaseController {

    @Autowired
    private AutoCodeService autoCodeService;

    @Autowired
    private TableConfigurationService tableConfigurationService;

    @GetMapping("/index")
    @RequiresPermissions("system:autoCode:index")
    @ApiOperation("主頁")
    public String index(){
        return "/system/autoCode/index";
    }

    /**
     * 保存配置
     * @param tableConfigurationEntity
     * @return
     */
    @PostMapping("/saveConfiguration")
    @ResponseBody
    @RequiresPermissions("system:autoCode:saveConfiguration")
    public RespEntity saveConfiguratoin(@RequestBody(required = false) TableConfigurationEntity tableConfigurationEntity){
        tableConfigurationService.insert(tableConfigurationEntity);
        return new RespEntity("配置保存成功");
    }

    /**
     * 查询所有的表
     * @return
     */
    @PostMapping("/getTables")
    @ResponseBody
    @RequiresPermissions("system:autoCode:getTables")
    public RespEntity getTables(){
        return new RespEntity("查询成功", autoCodeService.getTables());
    }

    /**
     * 查询选中表的详情
     * @return
     */
    @PostMapping("/queryTableDetail")
    @ResponseBody
    @RequiresPermissions("system:autoCode:queryTableDetail")
    public RespEntity queryTableDetail(String tableName){
        return new RespEntity("查询成功", autoCodeService.queryTableDetail(tableName));
    }

    /**
     * 单表生成
     * @return
     */
    @PostMapping("/codeGenerateBySingle")
    @ResponseBody
    @RequiresPermissions("system:autoCode:codeGenerateBySingle")
    public RespEntity codeGenerateBySingle(@RequestBody(required = false) TableConfigurationEntity tableConfigurationEntity){
//        tableConfigurationService.insert(tableConfigurationEntity);
        autoCodeService.codeGenerateBySingle(tableConfigurationEntity);
        return new RespEntity("查询成功");

    }
}
